home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / GUSI 1.4.1 / GUSI / include / GUSI_P.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-25  |  10.9 KB  |  424 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSI_P.h            -    Private stuff
  4. Author    :    Matthias Neeracher
  5. Language    :    MPW C/C++
  6.  
  7. $Log: GUSI_P.h,v $
  8. Revision 1.1  1994/02/25  02:57:01  neeri
  9. Initial revision
  10.  
  11. Revision 0.22  1993/07/17  00:00:00  neeri
  12. GUSIRingBuffer::proc -> defproc
  13.  
  14. Revision 0.21  1993/07/17  00:00:00  neeri
  15. GUSIO_MAX_DOMAIN -> AF_MAX
  16.  
  17. Revision 0.20  1993/06/27  00:00:00  neeri
  18. Socket::{pre,post}_select
  19.  
  20. Revision 0.19  1993/06/27  00:00:00  neeri
  21. Socket::ftruncate
  22.  
  23. Revision 0.18  1993/02/09  00:00:00  neeri
  24. Socket::lurking, Socket::lurkdescr
  25.  
  26. Revision 0.17  1993/01/31  00:00:00  neeri
  27. GUSIConfiguration::daemon
  28.  
  29. Revision 0.16  1993/01/17  00:00:00  neeri
  30. Destructors for Socketdomain
  31.  
  32. Revision 0.15  1993/01/17  00:00:00  neeri
  33. SAFESPIN
  34.  
  35. Revision 0.14  1993/01/03  00:00:00  neeri
  36. GUSIConfig
  37.  
  38. Revision 0.13  1992/09/24  00:00:00  neeri
  39. Include GUSIRsrc_P.h
  40.  
  41. Revision 0.12  1992/09/13  00:00:00  neeri
  42. SPINVOID didn't return
  43.  
  44. Revision 0.11  1992/08/30  00:00:00  neeri
  45. AppleTalkIdentity()
  46.  
  47. Revision 0.10  1992/08/03  00:00:00  neeri
  48. RingBuffer
  49.  
  50. Revision 0.9  1992/07/30  00:00:00  neeri
  51. Initializer Features
  52.  
  53. Revision 0.8  1992/07/26  00:00:00  neeri
  54. UnixSockets.choose()
  55.  
  56. Revision 0.7  1992/07/13  00:00:00  neeri
  57. Make AppleTalkSockets global
  58.  
  59. Revision 0.6  1992/06/27  00:00:00  neeri
  60. choose(), hasNewSF
  61.  
  62. Revision 0.5  1992/06/07  00:00:00  neeri
  63. Feature
  64.  
  65. Revision 0.4  1992/05/21  00:00:00  neeri
  66. Implemented select()
  67.  
  68. Revision 0.3  1992/04/19  00:00:00  neeri
  69. C++ rewrite
  70.  
  71. Revision 0.2  1992/04/18  00:00:00  neeri
  72. changed read/write/send/recv dispatchers
  73.  
  74. Revision 0.1  1992/04/18  00:00:00  neeri
  75. ppc Domain
  76.  
  77. *********************************************************************/
  78.  
  79. #include <GUSI.h>
  80. #include <GUSIRsrc_P.h>
  81. #include <TFileSpec.h>
  82.  
  83. extern "C" {
  84.  
  85. #include <sys/errno.h>
  86. #include <sys/uio.h>
  87. #include <sys/socket.h>
  88. #include <fcntl.h>
  89. #include <stdio.h>
  90. #include <string.h>
  91. #include <assert.h>
  92.  
  93. #include <Memory.h>
  94. #include <GestaltEqu.h>
  95. #include <Traps.h>
  96. #include <AppleEvents.h>
  97.  
  98. int         GUSI_error(int err);
  99. void *    GUSI_error_nil(int err);
  100. }
  101.  
  102. #pragma segment GUSI
  103.  
  104. #define GUSI_MAX_DOMAIN            AF_MAX
  105. #define DEFAULT_BUFFER_SIZE    4096
  106.  
  107. /*
  108.  *    In use and shutdown status.
  109.  */
  110. #define    SOCK_STATUS_USED        0x1        /* Used socket table entry */
  111. #define    SOCK_STATUS_NOREAD    0x2        /* No more reading allowed from socket */
  112. #define    SOCK_STATUS_NOWRITE    0x4        /* No more writing allowed to socket */
  113.  
  114. /*
  115.  *    Socket connection states.
  116.  */
  117. #define    SOCK_STATE_NO_STREAM        0    /* Socket doesn't have a MacTCP stream yet */
  118. #define    SOCK_STATE_UNCONNECTED    1    /* Socket is unconnected. */
  119. #define    SOCK_STATE_LISTENING        2    /* Socket is listening for connection. */
  120. #define    SOCK_STATE_LIS_CON        3    /* Socket is in transition from listen to connected. */
  121. #define    SOCK_STATE_CONNECTING    4    /* Socket is initiating a connection. */
  122. #define    SOCK_STATE_CONNECTED        5    /* Socket is connected. */
  123. #define    SOCK_STATE_CLOSING      6    /* Socket is closing */
  124.  
  125. #define        min(a,b)                ( (a) < (b) ? (a) : (b))
  126. #define        max(a,b)                ( (a) > (b) ? (a) : (b))
  127.  
  128. extern GUSISpinFn GUSISpin;
  129. extern "C" int GUSIDefaultSpin(spin_msg, long);
  130.  
  131. /* SPIN returns a -1 on user cancel for fn returning integers */
  132. #define        SPIN(cond,mesg,param)                            \
  133.                     do {                                                \
  134.                         if (GUSISpin)                            \
  135.                             if ((*GUSISpin)(mesg,param))    \
  136.                                 return GUSI_error(EINTR);        \
  137.                     } while(cond)
  138.  
  139. /* SPINP returns a NULL on user cancel, for fn returning pointers */                
  140. #define        SPINP(cond,mesg,param)                            \
  141.                     do {                                                \
  142.                         if (GUSISpin)                            \
  143.                             if ((*GUSISpin)(mesg,param)) {\
  144.                                 GUSI_error(EINTR);                \
  145.                                 return NULL;                        \
  146.                             }                                            \
  147.                     } while(cond)
  148.  
  149. /* SPINVOID just returns on user cancel, for fn returning void */                
  150. #define        SPINVOID(cond,mesg,param)                        \
  151.                     do {                                                \
  152.                         if (GUSISpin)                                \
  153.                             if ((*GUSISpin)(mesg,param)) {    \
  154.                                 GUSI_error(EINTR);                \
  155.                                 return;                                \
  156.                             }                                            \
  157.                     } while(cond)
  158.                     
  159. /* SAFESPIN doesn't return, you have to check errno */                
  160. #define        SAFESPIN(cond,mesg,param)                        \
  161.                     do {                                                \
  162.                         if (GUSISpin)                                \
  163.                             if ((*GUSISpin)(mesg,param)) {    \
  164.                                 GUSI_error(EINTR);                \
  165.                                 break;                                \
  166.                             } else {                                    \
  167.                                 errno = 0;                            \
  168.                             }                                            \
  169.                         else                                            \
  170.                             errno = 0;                                \
  171.                     } while(cond)
  172.                     
  173. class SocketTable;
  174.  
  175. class Socket {
  176.     friend class SocketTable;
  177.     
  178.     short            refCount;
  179. protected:
  180.     Boolean        lurking;
  181.     char            lurkDescr;
  182.     
  183.                     Socket();
  184. public:
  185.     virtual int    bind(void * name, int namelen);
  186.     virtual int connect(void * address, int addrlen);
  187.     virtual int listen(int qlen);
  188.     virtual Socket * accept(void * address, int * addrlen);
  189.     virtual int    read(void * buffer, int buflen);
  190.     virtual int write(void * buffer, int buflen);
  191.     virtual int recvfrom(void * buffer, int buflen, int flags, void * from, int * fromlen);
  192.     virtual int sendto(void * buffer, int buflen, int flags, void * to, int tolen);
  193.     virtual int getsockname(void * name, int * namelen);
  194.     virtual int getpeername(void * name, int * namelen);
  195.     virtual int getsockopt(int level, int optname, void *optval, int * optlen);
  196.     virtual int setsockopt(int level, int optname, void *optval, int optlen);
  197.     virtual int    fcntl(unsigned int cmd, int arg);
  198.     virtual int    ioctl(unsigned int request, void *argp);
  199.     virtual int    fstat(struct stat * buf);
  200.     virtual long lseek(long offset, int whence);
  201.     virtual int ftruncate(long offset);
  202.     virtual int    isatty();
  203.     virtual int shutdown(int how);
  204.     virtual void pre_select(Boolean wantRead, Boolean wantWrite, Boolean wantExcept);
  205.     virtual int select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  206.     virtual void post_select(Boolean wantRead, Boolean wantWrite, Boolean wantExcept);
  207.     virtual         ~Socket();
  208. };
  209.  
  210. class SocketDomain {
  211.     static SocketDomain *    domains[GUSI_MAX_DOMAIN];
  212. protected:
  213.     SocketDomain(int domain);
  214.     virtual ~SocketDomain();
  215. public:
  216.     inline static SocketDomain *    Domain(int domain);
  217.     
  218.     // Override *one* of the following two
  219.     
  220.     virtual Socket * open(const char * filename, int oflag);
  221.     virtual Socket * socket(int type, short protocol);
  222.     
  223.     // Optionally define the following
  224.     
  225.     virtual int choose(
  226.                         int         type, 
  227.                         char *     prompt, 
  228.                         void *     constraint,        
  229.                         int         flags,
  230.                          void *     name, 
  231.                         int *     namelen);
  232.     
  233.     // Never override the following
  234.     
  235.     virtual void DontStrip();
  236. };
  237.  
  238. class DeviceSocketDomain : public SocketDomain {
  239. protected:
  240.     friend class FileSocketDomain;
  241.     
  242.     static DeviceSocketDomain *    firstDev;
  243.     static const Socket *            TryNextDevice;
  244.     DeviceSocketDomain *                nextDev;
  245.  
  246.     DeviceSocketDomain(int domain);
  247. };
  248.  
  249. class SocketTable {
  250.     Socket *    sockets[GUSI_MAX_FD];
  251. public:
  252.     SocketTable();
  253.     ~SocketTable();
  254.     
  255.     int        Install(Socket * sock, int start = 0);
  256.     int        Remove(int fd);
  257.     Socket * operator[](int fd);
  258.     
  259.     void        Possess(int descr, Socket * sock);    // Install a daemon
  260. };
  261.  
  262. class FileSocketDomain : public SocketDomain {
  263.     friend SocketTable::SocketTable();
  264.     
  265.     virtual Socket * stdopen(int fd);
  266. public:
  267.     FileSocketDomain()    :    SocketDomain(AF_FILE)    {    }
  268.  
  269.     virtual Socket * open(const char * filename, int oflag);
  270.     virtual int choose(
  271.                         int         type, 
  272.                         char *     prompt, 
  273.                         void *     constraint,        
  274.                         int         flags,
  275.                          void *     name, 
  276.                         int *     namelen);
  277. };
  278.  
  279. struct GUSISuffix {
  280.     char         suffix[4];
  281.     OSType    suffType;
  282.     OSType    suffCreator;
  283. };
  284.  
  285. struct GUSIConfiguration {
  286.     OSType            defaultType;
  287.     OSType            defaultCreator;
  288.     
  289.     char                autoSpin;
  290.     
  291.     unsigned         noChdir         : 1;    // Set current directory without chdir()
  292.     unsigned         accurStat    : 1;    // Return # of subdirectories + 2 in st_nlink
  293.     unsigned         tcpDaemon    : 1;    // Works as a TCP daemon ?
  294.     unsigned         udpDaemon    : 1;    // Works as an UDP daemon ?
  295.     unsigned         filler2        : 4;
  296.     
  297.     OSType            version;
  298.     short                numSuffices;
  299.     GUSISuffix *    suffices;
  300.     
  301.     GUSIConfiguration();
  302.     
  303.     void SetDefaultFType(const TFileSpec & name) const;
  304.     void DoAutoSpin() const;
  305.     Boolean IsDaemon() const;
  306. private:
  307.     static Boolean firstTime;
  308.     static short    we;
  309. };
  310.  
  311. extern const GUSIConfiguration    GUSIConfig;
  312. extern SocketTable                    Sockets;
  313. extern FileSocketDomain             FileSockets;
  314.  
  315. typedef pascal OSErr (*OSErrInitializer)();
  316. typedef pascal void  (*voidInitializer)();
  317.  
  318. class Feature {
  319.     Boolean    good;
  320. public:
  321.     Feature(short trapNum, TrapType tTyp);
  322.     Feature(OSType type, long value);
  323.     Feature(OSType type, long mask, long value);
  324.     Feature(const Feature & precondition, OSErrInitializer init);
  325.     Feature(OSErrInitializer init);
  326.     Feature(const Feature & precondition, voidInitializer init);
  327.     Feature(voidInitializer init);
  328.     Feature(const Feature & cond1, const Feature & cond2);
  329.  
  330.     operator void*() const {    return (void *) good;    }
  331. };
  332.  
  333. extern Feature hasMakeFSSpec;
  334. extern Feature hasAlias;
  335. extern Feature    hasWNE;
  336. extern Feature hasNewSF;
  337. extern Feature hasProcessMgr;
  338. extern Feature hasCRM;
  339. extern Feature hasCTB;
  340. extern Feature hasStdNBP;
  341. extern Feature hasCM;
  342. extern Feature hasFT;
  343. extern Feature hasTM;
  344. extern Feature    hasPPC;
  345. extern Feature hasRevisedTimeMgr;
  346.  
  347. class ScattGath    {
  348.     Handle            scratch;
  349. protected:
  350.     void *            buf;
  351.     int                        len;
  352.     int                        count;
  353.     const struct iovec *    io;
  354.  
  355.     ScattGath(const struct iovec *iov, int cnt);
  356.     virtual ~ScattGath();
  357. public:
  358.     void *            buffer()            {    return buf;            }
  359.     int                buflen()            {    return len;            }
  360.     int                length(int l)    {    return len = l;    }
  361.     operator void *()                    {    return buf;            }
  362. };
  363.  
  364. class Scatterer : public ScattGath {
  365. public:
  366.     Scatterer(const struct iovec *iov, int count);
  367.     virtual ~Scatterer();
  368. };
  369.  
  370. class Gatherer : public ScattGath {
  371. public:
  372.     Gatherer(const struct iovec *iov, int count);
  373.     virtual ~Gatherer();
  374. };
  375.  
  376. typedef pascal void (*Deferred)(void *);
  377.  
  378. class RingBuffer {
  379.     // Valid bytes are between consume and produce
  380.     // Free bytes are between produce and consume
  381.     // bytes between endbuf-spare and endbuf are neither
  382.     Ptr        buffer;
  383.     Ptr        endbuf;
  384.     Ptr         consume;
  385.     Ptr        produce;
  386.     u_short    free;
  387.     u_short    valid;
  388.     u_short    spare;
  389.     Boolean    lock;
  390.     Deferred    defproc;
  391.     void *    arg;
  392.     
  393. public:
  394.                 RingBuffer(u_short bufsiz);
  395.                 ~RingBuffer();
  396.     
  397.     Ptr        Producer(long & len);            //    Find continuous memory for producer
  398.     Ptr        Consumer(long & len);            //    Find continuous memory for consumer
  399.     void        Validate(long len);                // Validate this, unallocate rest
  400.     void         Invalidate(long len);
  401.     void        Produce(Ptr from, long & len);//    Allocate, copy & validate
  402.     void        Consume(Ptr to, long & len);    // Copy & invalidate
  403.     
  404.     long        Free()                                { return free;                                    }        
  405.     long        Valid()                                { return valid;                                }
  406.     
  407.     void         Defer()                                { lock = true;                                    }
  408.     void         Undefer()                            { lock = false; if (defproc) defproc(arg);}
  409.     Boolean    Locked()                                { return lock;                                    }
  410.     void        Later(Deferred def, void * ar){ defproc = def; arg = ar;                    }
  411.     
  412.     operator void *()                                { return buffer;                                }
  413. };
  414.  
  415. Boolean GUSIInterrupt();
  416.  
  417. Boolean CopyIconFamily(short srcResFile, short srcID, short dstResFile, short dstID);
  418.  
  419. pascal OSErr PPCInit_P();
  420.  
  421. OSErr AppleTalkIdentity(short & net, short & node);
  422.  
  423. pascal OSErr GetOffMyCloud(AppleEvent*, AppleEvent*, long); // We don't want this aevt
  424.